<template>
  <el-drawer v-model="isShowDialog" size="60%" title="触发器执行记录">
    <div class="table-box">
      <ProTable
        ref="proTableRef"
        title="触发器执行记录"
        :columns="columns"
        :data-callback="dataCallback"
        :request-api="query"
        :tool-button="false"
      >
      </ProTable>
    </div>
  </el-drawer>
</template>
<script lang="tsx" setup name="jobTriggerRecord">
import jobApi from "@/api-services/apis/system/jobApi";
import { SysJobTrigger } from "@/api-services/models/system";
import { SqlSugarPagedList, UnifyResult } from "@/api-services/unifyResult";
import ProTable from "@/components/ProTable/index.vue";
import { ColumnProps, ProTableInstance } from "@/components/ProTable/interface";
import { jobStatusDict } from "../enum/jobDict";
const proTableRef = ref<ProTableInstance>();
const isShowDialog = ref(false);
export interface JobTriggerRecordProps {
  jobId?: String;
  triggerId?: String;
}
const jobTriggerRecordProps = ref<JobTriggerRecordProps>({
  jobId: "",
  triggerId: ""
});
const query = async (params: any) => {
  if (!jobTriggerRecordProps.value.jobId) return null;
  let newParams = JSON.parse(JSON.stringify(params));
  newParams.jobId = jobTriggerRecordProps.value.jobId;
  newParams.triggerId = jobTriggerRecordProps.value.triggerId;
  return await jobApi.getPageJobTriggerRecord(newParams);
};
const dataCallback = (queryRes: UnifyResult<SqlSugarPagedList<SysJobTrigger.SysJobTriggerRecord>>) => {
  const data = queryRes.data;
  return {
    items: data.items,
    totalCount: data.totalCount,
    pageIndex: data.pageIndex,
    pageSize: data.pageSize
  };
};
const columns: ColumnProps<SysJobTrigger.SysJobTriggerRecord>[] = [
  {
    prop: "jobId",
    label: "作业编号",
    width: 200
  },
  { prop: "triggerId", label: "触发器编号", width: 200 },
  { prop: "numberOfRuns", label: "当前运行次数", width: 200 },
  { prop: "lastRunTime", label: "最近运行时间", width: 200 },
  {
    prop: "nextRunTime",
    label: "下一次运行时间",
    width: 200
  },
  {
    prop: "status",
    label: "触发器状态",
    width: 100,
    render: scope => {
      let status = scope.row.status;
      if (!status) return;
      let typeDict = jobStatusDict[status];
      return <el-tag type={typeDict?.type}>{typeDict?.label}</el-tag>;
    }
  },
  { prop: "result", label: "执行结果", width: 200 },
  { prop: "elapsedTime", label: "耗时", width: 200 },
  { prop: "createdTime", label: "创建时间", width: 200 }
];

const open = (val: JobTriggerRecordProps) => {
  jobTriggerRecordProps.value = val;
  isShowDialog.value = true;
  proTableRef.value?.getTableList();
};
// 导出对象W
defineExpose({ open });
</script>
